package SearchTable;

import java.lang.reflect.Array;

public class StaticSearchTable {

    //设置"哨兵"
    public static int search9_1(int[] array,int value){
        array[0]=value;
        int start=array.length-1;
        int i;
        for (i=start;array[i]!=value;i--){
        }
        return i;
    }
    public static int con_search9_1(int[] array,int value){
        //想对比一下二者在查找的时候花费的时间是否差一倍，但是失败了emmm
        int start=array.length-1;
        int i;
        for (i=start;array[i]!=value&&i>=1;i--){
        }
        return i;
    }

    //折半查找
    public static int Search_Bin(int[] array,int value){
        //折半查找，需要为有序表，且为顺序存储
        int low=0;
        int high=array.length-1;
        while (low<=high){
            int mid=(low+high)/2;
            if (array[mid]==value){
                return mid;
            }else if (value<=array[mid]){
                high=mid-1;
            }else {
                low=mid+1;
            }
        }
        return 0;
    }
    public static void main(String[] args)  {
        int[] arr={1,4,6,8,9,23,45,67,89};
        System.out.println(Search_Bin(arr,7));
    }
}
